module Sparkle
  module Controller

    def fetch
      cache_entry = Rails.cache.read(params[:key])
      partial = cache_entry[:partial]
      locals = cache_entry[:locals]
      begin
        locals.values.each { |value| value._sparkle_refresh! if value.class.include?(Refreshable) }
      rescue Exception => e
        session[:sparkle_exception] = e
      end
      render :partial => partial, :locals => locals.merge(:sparkle_cache_key => nil)
      raise session.delete(:sparkle_exception) if session[:sparkle_exception]
    end

  end
end
